#include "defs.h"
#include "filterdefs.h"
+#include "inifile.h"
typedef struct {
filter_vecs_t *vec;
continue;
}
+ /* step 1: initialize by inifile or default values */
+ if (vec->vec->args) {
+ for (ap = vec->vec->args; ap->argstring; ap++) {
+ char *temp;
+
+ temp = inifile_readstr(global_opts.inifile, vec->name, ap->argstring);
+ if (temp == NULL) temp = inifile_readstr(global_opts.inifile, "Common filter settings", ap->argstring);
+ if (temp == NULL) temp = ap->defaultvalue;
+ assign_option(vec->name, ap, temp);
+ }
+ }
+
+ /* step 2: override settings with command-line values */
res = strchr(vecname, ',');
if (res) {
*opts = res+1;
if (vec->vec->args) {
for (ap = vec->vec->args; ap->argstring; ap++){
- char *opt = get_option(*opts,
- ap->argstring);
- if ( opts ) {
- *ap->argval = opt;
- }
- else if ( ap->defaultvalue ) {
- *ap->argval = xstrdup(
- ap->defaultvalue);
- }
- else {
- *ap->argval = NULL;
- }
- }
- }
- } else {
- *opts = NULL;
- if (vec->vec->args) {
- for (ap = vec->vec->args; ap->argstring; ap++){
- if ( ap->defaultvalue ) {
- *ap->argval = xstrdup(
- ap->defaultvalue);
- }
- else {
- *ap->argval = NULL;
+ char *opt;
+
+ opt = get_option(*opts, ap->argstring);
+ if ( opt ) {
+ assign_option(vec->name, ap, opt);
+ xfree(opt);
}
}
}
}
+ if (global_opts.debug_level >= 1)
+ disp_vec_options(vec->name, vec->vec->args);
+
xfree(v);
return vec->vec;
if ( fvec->args ) {
for ( ap = fvec->args; ap->argstring; ap++) {
- if (ap->argval && *ap->argval) xfree(*ap->argval);
+ if (ap->argval && *ap->argval) {
+ xfree(*ap->argval);
+ *ap->argval = NULL;
+ }
}
}
}
#define START_BUFSIZE 257
#define DELTA_BUFSIZE 128
+#define GPSBABEL_INIFILE "gpsbabel.ini"
+
+static FILE *
+try_open_gpsbabel_inifile(const char *path) /* can be empty or NULL */
+{
+ FILE *result;
+ char *buff;
+ int len;
+
+ if (path == NULL) return NULL;
+
+ len = strlen(path);
+ buff = xmalloc(len + 1 + strlen(GPSBABEL_INIFILE) + 1);
+ strcpy(buff, path);
+ if (len > 0) {
+ char test = buff[len - 1];
+#ifdef __WIN32__
+ if ((test != '\\') && (test != ':'))
+ strcat(buff, "\\");
+#else
+ if (test != '/')
+ strcat(buff, "/");
+#endif
+ }
+ strcat(buff, GPSBABEL_INIFILE);
+ result = fopen(buff, "r");
+ xfree(buff);
+
+ return result;
+}
+
+static FILE *
+open_gpsbabel_inifile(void)
+{
+ FILE *res;
+ char *envstr;
+
+ envstr = getenv("GPSBABELINI");
+ if (envstr != NULL) {
+ res = fopen(envstr, "r");
+ if (res == NULL) {
+ warning("WARNING: GPSBabel-inifile, defined in environment, NOT found!\n");
+ return NULL;
+ }
+ return res;
+ }
+ res = try_open_gpsbabel_inifile(""); /* PWD */
+ if (res == NULL) {
+#ifdef __WIN32__
+ res = try_open_gpsbabel_inifile(getenv("APPDATA"));
+ if (res == NULL) res = try_open_gpsbabel_inifile(getenv("WINDIR"));
+ if (res == NULL) res = try_open_gpsbabel_inifile(getenv("SYSTEMROOT"));
+#else
+ if ((envstr = getenv("HOME")) != NULL) {
+ char *path;
+
+ path = xmalloc(strlen(envstr) + 11);
+ strcpy(path, envstr);
+ strcat(path, "/.gpsbabel");
+ res = try_open_gpsbabel_inifile(path);
+ xfree(path);
+ }
+ if (res == NULL) res = try_open_gpsbabel_inifile("/usr/local/etc");
+ if (res == NULL) res = try_open_gpsbabel_inifile("/etc");
+#endif
+ if (res == NULL) return NULL;
+ }
+}
+
static void
inifile_load_file(FILE *fin, inifile_t *inifile, const char *myname)
{
}
cin = lrtrim(buf);
- if (*cin == '\0') continue;
+ if (*cin == '\0') continue; /* skip empty lines */
+ if ((*cin == '#') || (*cin == ';')) continue; /* skip comments */
if (*cin == '[')
{
{
queue *elem, *tmp;
+ if (inifile == NULL) return NULL;
+
QUEUE_FOR_EACH(&inifile->secs, elem, tmp)
{
inifile_section_t *sec = (inifile_section_t *) elem;
inifile_init:
reads inifile filename into memory
myname represents the calling module
+
+ filename == NULL: try to open global gpsbabel.ini
*/
inifile_t *
inifile_init(const char *filename, const char *myname)
{
inifile_t *result;
- FILE *fin;
+ FILE *fin = NULL;
- fin = xfopen(filename, "r", myname);
+ if (filename == NULL) {
+ fin = open_gpsbabel_inifile();
+ if (fin == NULL) return NULL;
+ }
+ else fin = xfopen(filename, "r", myname);
result = xcalloc(1, sizeof(*result));
QUEUE_INIT(&result->secs);
#include "filterdefs.h"
#include "cet.h"
#include "cet_util.h"
+#include "inifile.h"
#include <ctype.h>
+#define MYNAME "main"
+
static void
usage(const char *pname, int shorter)
{
" (without the quotes) are all valid file type specifications.\n"
"\n"
"Options:\n"
+" -p Prereferences file (gpsbabel.ini)\n"
" -s Synthesize shortnames\n"
" -r Process route information\n"
" -t Process track information\n"
global_opts.objective = wptdata;
global_opts.masked_objective = NOTHINGMASK; /* this makes the default mask behaviour slightly different */
- global_opts.charset = NULL; /* #ifdef UTF8_SUPPORT */
- global_opts.charset_name = NULL; /* #ifdef UTF8_SUPPORT */
+ global_opts.charset = NULL;
+ global_opts.charset_name = NULL;
+ global_opts.inifile = NULL;
gpsbabel_now = time(NULL); /* gpsbabel startup-time */
gpsbabel_time = current_time(); /* same like gpsbabel_now, but freezed to zero during testo */
}
debug_mem_output( "\n" );
#endif
+ if (gpsbabel_time != 0) { /* within testo ? */
+ global_opts.inifile = inifile_init(NULL, MYNAME);
+ }
cet_register();
waypt_init();
case 'l':
cet_disp_character_set_names(stdout);
exit(0);
+ case 'p':
+ optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn];
+ inifile_done(global_opts.inifile);
+ if (strcmp(optarg, "") == 0) /* from GUI to preserve inconsistent options */
+ global_opts.inifile = NULL;
+ else
+ global_opts.inifile = inifile_init(optarg, MYNAME);
+ break;
}
}
route_flush_all();
exit_vecs();
exit_filter_vecs();
+ inifile_done(global_opts.inifile);
#ifdef DEBUG_MEM
debug_mem_close();